package com.yazi.mxz.mobile.controller.user;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Shopping;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.service.ShoppingService;
import com.yazi.mxz.common.service.StoreGoodsService;

@Controller("user-shoppingController")
@RequestMapping("/user/shopping")
public class ShoppingController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(ShoppingController.class);
	
	@Resource
	ShoppingService shoppingService;
	@Resource
	StoreGoodsService storeGoodsService;
	
	@RequestMapping("/updateDo/json")
	@ResponseBody
	public StatusResult updateDoJson(long shoppingId, float buyCount) {
		log.debug("更新购物车商品购买量");
		StatusResult jsonResult = new StatusResult();
		try{
			shoppingService.editBuyCountByMemberId(getMemberId(), shoppingId, buyCount);
			jsonResult.setOkMessage("操作成功！");
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			log.error("更新购物车商品购买量异常：", e);
			jsonResult.setErrorMessage("操作失败！");
		}
		return jsonResult;
	}
	
	@RequestMapping("addDo")
	public String addDo(Shopping shopping, Model model) {
		log.debug("购物车添加商品");
		// 获取添加到购物车商品，取其分类ID返回页面
		StoreGoods storeGoods;
		Long catId;
		try{
			shoppingService.addByMember(getMember(), shopping);
			storeGoods = storeGoodsService.one(shopping.getStoreGoods().getStoreGoodsId());
			catId = storeGoods.getCategory().getCatId();
			
		} catch (MyException e) {
			log.debug("购物车添加商品失败：{}", e.getMessage());
			model.addAttribute(TIP_DATA, e);
			return FORWARD_TIP;
		} catch (Exception e) {
			log.error("购物车添加商品异常：", e);
			model.addAttribute(TIP_MSG, "加入购物车失败，请重试");
			return FORWARD_TIP;
		} 
		
		model.addAttribute("catId", catId);
		return "user/shopping_addDo";
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long shoppingId) {
		log.debug("删除购物车商品");
		StatusResult jsonResult = new StatusResult();
		try{
			shoppingService.removeByMemberId(getMemberId(), new Long[]{shoppingId});
			jsonResult.setOkMessage("删除成功");
		} catch (Exception e) {
			log.error("删除购物车商品异常:", e);
			jsonResult.setErrorMessage("删除失败");
		}
		return jsonResult;
	}
	
	@RequestMapping("/count/json")
	@ResponseBody
	public ObjectResult<?> countJson(long storeGoodsId) {
		log.debug("获取某物品在购物车中已有数量");
		ObjectResult<Float> jsonResult = new ObjectResult<Float>();
		try{
			Float count = shoppingService.countOfStoreGoodsId(getMemberId(), storeGoodsId);
			jsonResult.setResult(count);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取购物车物品信息异常:", e);
			jsonResult.setErrorMessage("物品数量加载失败");
		}
		return jsonResult;
	}
	
	@RequestMapping("/amount/json")
	@ResponseBody
	public ObjectResult<?> amountJson() {
		log.debug("获取购物车中商品种数");
		ObjectResult<Integer> jsonResult = new ObjectResult<Integer>();
		try{
			Integer amount = shoppingService.countOfMemberId(getMemberId());
			jsonResult.setResult(amount);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取购物车中商品种数异常:", e);
			jsonResult.setErrorMessage("购物车商品种数加载失败");
		}
		return jsonResult;
	}
	
}
